<?php

namespace app\common\cachemodel\chat;

use app\common\lib\exception\ApiException;
use app\common\model\BaseCache;
use think\Exception;
use XznsTools\cache\CacheManage;

class HzQuestionText extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.hz_question_text';

    protected $cacheModel = 'explain.question_text';

    protected $textCacheData = [];

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array
     * @throws ApiException
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids], 'status' => 1])->column('logic_id,specific_value,content', 'id');
        }
        catch (Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 通过ID 获取词条
     * @param int $id
     * @return mixed
     * @throws \Exception
     */
    public function getTextById(int $id)
    {
        if (!isset($this->textCacheData[$id]))
        {   // 数据存在 直接获取
            $this->textCacheData[$id] = $this->getInfoById($id);
        }

        return $this->textCacheData[$id];
    }

    /**
     * 批量 获取 逻辑信息
     * @param $ids
     * @return mixed
     * @throws \Exception
     */
    public function batchGetDataByLogicId($ids)
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);

        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheType('string')
            ->batchGetCache($ids, function ($ids)
            {
                try
                {
                    $data = $this->where(['logic_id' => ['in', $ids], 'status' => 1])->order('id asc')
                        ->column('id,logic_id,specific_value', 'id');

                    $resData = [];
                    foreach ($data as $item)
                    {
                        $resData[$item['logic_id']][] = [
                            'id' => (int)$item['id'],
                            'logic_id' => (int)$item['logic_id'],
                            'specific_value' => $item['specific_value'],
                        ];
                    }

                    foreach ($resData as $k => $v)
                    {
                        $resData[$k] = json_encode($v, JSON_UNESCAPED_UNICODE);
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                    return [];
                }
            }, [], 'list_logic_details');

        foreach ($data as $index => $itemData)
        {
            $data[$index] = json_decode($itemData, true);
        }

        $dataList = array_merge(...$data);
        // 重新排序    排序规则   pid asc,id asc
        array_multisort(array_column($dataList, 'id'), SORT_ASC, $dataList);

        return $dataList;
    }
}